<template>
	<skeleton :skeleton="{name:'history'}" :showSkeleton="showSkeleton">
		<block slot="content">
			<noData :showErrorTip="noData">
				<view slot='content' class="notice">
					<view @click="navigateToDetail(item,index)" class="content" v-for="(item, index) in msgData" :key="index">
						<view class="content__title">
							<text class="content__title-text">{{ item.content }}</text>
							<text class="content__title-time">{{item.sendTime}}</text>
							<image class="delete" src="/static/study/del.png" @click.stop="navigateToDetail(item,index)"/>
						</view>
					</view>
					<view v-if="noMore" class="loading">没有更多了</view>
				</view>
			</noData>
		</block>
	</skeleton>
</template>

<script>
	import {isEmpty} from '@/utils/index';
	import msgService from '@/services/msg';
	const service = new msgService();
	export default {
		data() {
			return {
				showSkeleton: true,
				// noData: false,
				noMore: false,
				pageSize: 20,
				pageNum: 1,
				msgData: [],
				curIndex:-1
			};
		},
		computed:{
			noData(){
				return isEmpty(this.msgData);
			}
		},
		onNavigationBarButtonTap() {
			uni.navigateTo({
				url: "/pages/message/createMsg"
			});
		},
		onLoad() {
			this.loadData(1);
			uni.$on('itemDelete',(payload)=>{
				console.log('itemDel');
				this.msgData.splice(this.curIndex,1);
				this.curIndex = -1;
			});
			uni.$on('create',()=>{
				this.loadData(1);
			})
		},
		onUnload() {
			uni.$off('itemDelete');
			uni.$off('create');
		},
		onPullDownRefresh() {
			this.loadData(1);
		},
		onReachBottom() {
			this.loadData(this.pageNum+1);
		},
		methods: {
			navigateToDetail(item,index) {
				this.curIndex = index;
				uni.navigateTo({
					url: `/pages/message/outDetail?id=${item.id}`
				});
			},
			loadData(pageNum) {
				const param = {
					pageSize: this.pageSize,
					pageNum: pageNum == undefined ? 1 : pageNum,
				};
				service.getMyCreateMail(param).then(response => {
					uni.stopPullDownRefresh();
					if (response.success) {
						if (pageNum == 1) {
							this.msgData = response.result.list;
							// this.noData = isEmpty(this.msgData);
							this.noMore = false;
						} else {
							this.msgData = [...this.msgData, ...response.result.list];
							this.noMore = !response.result.hasNextPage;
						}
						this.pageNum = pageNum;
					} else {
						uni.showToast({
							title: response.message,
							duration: 2000,
							icon: 'none'
						})
					}
				}).then(()=>{
					this.showSkeleton = false;
				});
			}
		}
	};
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.content {
		background-color: #fff;
		padding: 30upx 0upx;
		margin: 0 10px;
		margin-bottom: 20upx;
		border-bottom: 1px solid #f1f1f1;

		&__title {
			display: flex;
			color: #333333;

			&-text {
				flex: 1;
				font-size: 28upx;
				/*font-weight: 600;*/
				color: #333333;
				overflow: hidden;
				text-overflow: ellipsis;
				-webkit-line-clamp: 1;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				line-height: 48upx;

			}

			&-time {
				text-align: right;
				font-size: 20upx;
				color: #999;
				line-height: 2;
			}
		}

		&__introduce {
			font-size: 28upx;
			color: #333333;
			margin-top: 10upx;
		}
	}
	.delete{
		width: 40upx;
		height: 40upx;
		margin-left: 20upx;
	}
	.loading {
		text-align: center;
		font-size: 28upx;
		padding: 20upx 0;
		color: #999;
	}
</style>
